import os
import re
import sys
from pathlib import Path
import django

BASE_DIR = Path(__file__).resolve().parent
sys.path.append(os.path.dirname(BASE_DIR))
sys.path.append(os.path.dirname(BASE_DIR) + '\\venv\\Lib\\site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "base_django_api.settings")
django.setup()

from user.models import Auth, AuthPermission, Permission

# 权限 初始化代码
# 主要生成 所有查看权限
# 生成 所有查看，所有新建权限
# 生成 所有查看，所有新建，所有编辑权限

from base_django_api.urls import router
print('------------------------------------生成 permission 权限列表, 用于角色权限勾选-------------------------------')
permisssion = {}

for i in router.urls:
    print(i.__dict__)
    # print(i.pattern.__dict__['_regex'])
    result = re.search(r'[a-zA-Z]{2,}', i.pattern.__dict__['_regex'])
    # print(result.group())
    if result is not None:
        permisssion[result.group()] = {'object_name': result.group(), 'object_name_cn': i.name}

for key, values in permisssion.items():
    # 判断权限是否存在，已经有的权限不重复生成
    perm = Permission.objects.filter(object_name=values['object_name'])
    if len(perm) <= 0:
        per = Permission.objects.create(object_name=values['object_name'], object_name_cn=values['object_name_cn'])
        print(per.__dict__)
    else:
        print('跳过生成权限，' + values['object_name'] + '权限已存在')
print('---------------------------------------生成auth permission 角色权限 --------------------------------------')
url = {}

for i in router.urls:
    # print(i.__dict__)
    # print(i.pattern.__dict__['_regex'])
    result = re.search(r'[a-zA-Z]{2,}', i.pattern.__dict__['_regex'])
    # print(result.group())
    if result is not None:
        url[result.group()] = result.group()

print('---------------------------------------所有权限url---------------------------------------------------------')
for key, values in permisssion.items():
    print(key, values['object_name_cn'])

# print('----------------------------------------------删除原来的权限--------------------------------------------------')
# auths = Auth.objects.all()
# AuthPermissions = AuthPermission.objects.all()
# for j in auths:
#     j.delete()
# for o in AuthPermissions:
#     o.delete()
#
# print('----------------------------------------------所有查看权限--------------------------------------------------')
# auth = Auth.objects.create(auth_type="查看权限")
# for key, values in permisssion.items():
#     AuthPermission.objects.create(auth=auth, object_name=key, object_name_cn=values['object_name_cn'], auth_list=True)
#
# print('----------------------------------------------所有查看，新增权限--------------------------------------------------')
# auth1 = Auth.objects.create(auth_type="查看,新增权限")
# for key, values in permisssion.items():
#     AuthPermission.objects.create(auth=auth1, object_name=key, object_name_cn=values['object_name_cn'], auth_list=True, auth_create=True)
#
# print('----------------------------------------------所有查看,新增,编辑权限-----------------------------------------------')
# auth2 = Auth.objects.create(auth_type="查看,新增,编辑权限")
# for key, values in permisssion.items():
#     AuthPermission.objects.create(auth=auth2, object_name=key, object_name_cn=values['object_name_cn'], auth_list=True, auth_create=True,
#                                   auth_update=True)
